package cn.easyar.samples.helloarqrcode.remote;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import butterknife.ButterKnife;
import cn.easyar.samples.helloarqrcode.R;


public abstract class BaseActivity<T> extends AppCompatActivity {
    public Activity mActivity;
    private InputMethodManager mImm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /*是否全屏*/
        if(isFullScreen()){
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }


        setContentView(R.layout.base_activity);
        FrameLayout fl_base_view = findViewById(R.id.fl_base_view);
        RelativeLayout rl_root = findViewById(R.id.rl_root);

        if (isCanRequestedOrientation()) { //设置横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        mActivity = this;

        fl_base_view.addView(LayoutInflater.from(mActivity).inflate(getLayoutId(), rl_root, false));

        ButterKnife.bind(this);
        initView(savedInstanceState);
        loadData();

    }




    public abstract int getLayoutId();
    public abstract void initView(Bundle savedInstanceState);
    public abstract void loadData();


    /**
     * 强制横屏
     * @return
     */
    protected boolean isCanRequestedOrientation() {
        //默认设置横屏 可以重写
        return true;
    }

    /**
     * 设置全屏
     * @return
     */
    protected boolean isFullScreen() {
        //默认设置全屏 可以重写
        return true;
    }

    public  boolean isHide(){
        return  true;
    }



    /**
     * 点击其他区域隐藏键盘
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)&&isHide()) {
                mImm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (mImm != null) {

                    mImm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }

            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return super.dispatchTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.gc();
    }

    protected void tsg(String s){
//        ToastCommom.createToastConfig().ToastShow(mActivity,s);
    }
}
